<?php

namespace App\Admin\Controllers;

use App\Models\Student;
use App\Models\Squad;
use App\Models\Class_type;
use App\Models\Attendance_cycle;
use App\Models\Test_config;
use App\Models\Student_attendance;
use App\Models\Student_mark;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class My_studentController extends Controller
{
    use HasResourceActions;
    //public $squad_id;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content->header(trans('admin.student'))
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.student'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.student'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    public function attendance_entering($id, Content $content)
    {
        return $content
            ->header(trans('admin.student_attendance'))
            ->description(trans('admin.detail'))
            ->body(Admin::show(Student::findOrFail($id), function (Show $show) {
                $show->panel()->tools(function ($tools){
                    $tools->disableDelete();
                    $tools->disableEdit();
                    $tools->disableList();
                });
                $show->no('学号');
                $show->ch_name('中文名');
                $show->en_name('英文名');
                $show->squad()->name('所在班级');
                
                $show->student_attendance('考勤记录', function ($line) {
                    $line->setResource('/admin/student_attendance');
                    $line->model()->orderBy('id', 'desc');
                    $line->id(trans('admin.ID'))->sortable();
                    $line->attendance_cycle()->name('考勤周期');

                    $line->class_period(trans('admin.class_period'));
                    $line->attendance_start_date(trans('admin.attendance_start_date'));
                    $line->attendance_end_date(trans('admin.attendance_end_date'));
                    $line->classroom()->name(trans('admin.classroom'));
                    $line->attendance_status(trans('admin.status'))->editable('select', ['1' => '正常', '2' => '请假','3' => '迟到', '4' => '缺勤'])->sortable();
                    $line->actions(function ($actions) {
                        $actions->disableView();
                    });
                    $filename="考勤列表";
                    $line->exporter(new ExcelExpoter($line,$filename));

                    $line->filter(function ($filter) {
                        $filter->column(1/2, function ($filter) {
                            $filter->disableIdFilter();
                            $filter->equal('attendance_cycle_id', trans('admin.attendance_cycle_id'))->select(Attendance_cycle::where('status','=','0')->get()->pluck('name', 'id'));
                            $filter->equal('attendance_status', trans('admin.attendance_status'))->radio([
                                '1'     => '正常',
                                '2'     => '请假',
                                '3'     => '迟到',
                                '4'     => '缺勤'
                            ]);
                        });
                        $filter->column(1/2, function ($filter) {
                            $filter->equal('class_period',trans('admin.class_period'))->integer();
                        });
                    });
                });
        }));
    }
    public function mark_entering($id, Content $content)
    {
        return $content
            ->header(trans('admin.student_mark'))
            ->description(trans('admin.detail'))
            ->body(Admin::show(Student::findOrFail($id), function (Show $show) {
                $show->panel()->tools(function ($tools){
                    $tools->disableDelete();
                    $tools->disableEdit();
                    $tools->disableList();
                });
                $show->no('学号');
                $show->ch_name('中文名');
                $show->en_name('英文名');
                $show->squad()->name('所在班级');
                
                $show->student_mark('成绩记录', function ($line) {
                    $line->setResource('/admin/student_mark');
                    $line->model()->orderBy('id', 'desc');
                    $line->id(trans('admin.ID'))->sortable();
                    $line->test_id(trans('admin.test_name'))->display(function($test_id){
                        return Test_config::find($this->test_id)->name;
                    });

                    $line->test_batch(trans('admin.test_batch'))->editable();
                    $line->test_score(trans('admin.test_score'))->editable();
                    
                    $line->actions(function ($actions) {
                        $actions->disableView();
                    });
                    $filename="成绩单";
                    $line->exporter(new ExcelExpoter($line,$filename));

                    $line->filter(function ($filter) {
                        $filter->column(1/2, function ($filter) {
                            $filter->disableIdFilter();
                            $filter->equal('test_id', trans('admin.test_name'))->select(Test_config::where('status','=','0')->get()->pluck('name', 'id'));
                        });
                        $filter->column(1/2, function ($filter) {
                            $filter->equal('test_batch',trans('admin.test_batch'))->integer();
                        });
                    });
                });
        }));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Student());
        $squad_id = request('squad_id');

        $grid->model()->where('squad_id', '=', $squad_id);
        $grid->model()->orderBy('squad_id', 'desc');
        $grid->id(trans('admin.ID'))->sortable();
        $grid->no(trans('admin.student_no'))->sortable();
        $grid->ch_name(trans('admin.ch_name'));
        $grid->en_name(trans('admin.en_name'));
        $grid->sex(trans('admin.sex'))->display(function ($sex) {
            if($this->sex==1){
                return trans('admin.men');
            }elseif ($this->sex==2) {
                return trans('admin.women');
            }else{
                return 'N/A';
            }
        })->sortable();
        $grid->squad_id(trans('admin.squad_id'))->display(function($squad_id){
            if ($this->squad_id==0) {
                return 'N/A';
            }else{
                $type_id = Squad::find($this->squad_id)->type;
                $type = Class_type::find($type_id)->name;
                $no = Squad::find($this->squad_id)->no;
                return $type.''.$no;
            }
        })->sortable();

        $grid->grade('年级')->editable();
        $grid->school('学校')->editable();

        $grid->enrollment_time(trans('admin.enrollment_time'))->display(function (){
                return date('Y/m/d',strtotime($this->enrollment_time));
        })->sortable();
        
        $grid->status(trans('admin.status'))->display(function ($status) {
            if($this->status==1){
                return trans('admin.zaidu');
            }elseif ($this->status==2) {
                return trans('admin.xiuxue');
            }elseif ($this->status==3) {
                return trans('admin.tuixue');
            }else{
                return 'N/A';
            }
        })->sortable();

        $filename="学生列表";

        $grid->exporter(new ExcelExpoter($grid,$filename));

        $grid->tools(function ($tools) { 
            //关闭批量删除 
            $tools->batch(function ($batch) { 
                $batch->disableDelete(); 
            });
        });

        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->disableCreateButton();

        //筛选弹出框字段
        $grid->filter(function ($filter) {
            $filter->column(1/2, function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('no',trans('admin.student_no'));
                $filter->like('en_name',trans('admin.en_name'));
                $filter->between('enrollment_time', trans('admin.enrollment_time'))->datetime(); 
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('ch_name',trans('admin.ch_name'));
                $filter->equal('sex', trans('admin.sex'))->select($options = ['1'=> trans('admin.men') , '2'=> trans('admin.women')]);
                $filter->equal('status', trans('admin.status'))->select($options = ['1'=> trans('admin.zaidu') , '2'=> trans('admin.xiuxue'), '3'=> trans('admin.tuixue')]);
            });
        });
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Student());

        $form->display('id', 'ID');
        $form->text('no', trans('admin.student_no'))->rules('required')->addElementLabelClass('must-input');
        $form->text('ch_name', trans('admin.ch_name'))->rules('required')->addElementLabelClass('must-input');
        $form->text('en_name', trans('admin.en_name'))->rules('required')->addElementLabelClass('must-input');
        $form->radio('sex', trans('admin.sex'))->options(['1'=> trans('admin.men'),'2'=> trans('admin.women')])->rules('required')->addElementLabelClass('must-input');
        $form->hidden('squad_id')->default(request('squad_id'));
        $form->date('enrollment_time', trans('admin.enrollment_time'))->format('YYYY-MM-DD')->rules('required')->addElementLabelClass('must-input');
        $form->radio('status', trans('admin.status'))->options(['1' => trans('admin.zaidu'), '2'=> trans('admin.xiuxue'),'3'=> trans('admin.tuixue')])->default('1');
        $form->text('grade','年级');
        $form->text('school','学校');

        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->saved(function (Form $form) {
            $squad_id = $form->model()->squad_id;
            // 跳转页面
            return redirect('/admin/my_squad/'.$squad_id);
        });

        return $form;
    }
}
